/*
 * HomePage.java
 *
 * Created on July 28, 2008, 4:44 PM
 */

package schedule_master;

import javax.swing.JFrame;
import java.sql.*;
import java.util.ArrayList;
import javax.swing.JComboBox;
import java.awt.Cursor;
/**
 *
 * @author  PGSIST
 */
public class HomePage extends javax.swing.JPanel {
    Schedule_MasterView smv;
    /** Creates new form HomePage */
    String[] filters = new String[4];
    public HomePage(Schedule_MasterView smv1) {
        smv=smv1;
        initComponents();
        jPanel1.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
    }
    
    public JComboBox getDepartmentComboBox(){
       ArrayList<String> departments = new ArrayList<String>();
       departments.add("Department");
       // departments.add("Department");
        // Try to access the database, otherwise, catch the exception
        try{
            String driver = "com.mysql.jdbc.Driver";
            Class.forName(driver);
        
            String url = "jdbc:mysql://pgsist.org:3306/2008_itp0";
            String userNameSQL = "itp0";
            String passwordSQL = "frebre3w";
            String deptIDQuery = "Select deptID " +
                                 "FROM tblDepartments " +
                                 "WHERE true";
            String deptNameQuery = "Select deptName " +
                               "FROM tblDepartments " +
                               "WHERE 1";
            
            
            System.out.println("Connecting to database...");
            Connection conn = DriverManager.getConnection(url, userNameSQL, passwordSQL);
            System.out.println("Done.");
        
            Statement st = conn.createStatement();
            
            ResultSet res = st.executeQuery(deptIDQuery);
            
            // Count the number of departments
            int i = 1;
            
            // While there is an item in deptID column of tblDepartments
            while(res.next()){
                // Add the current deptID to department array of current index
                departments.add( res.getString(1));
                // Increment the index of the array
                i++;
            } 
            
            // Set i back to zero
            i = 1;
            res = st.executeQuery(deptNameQuery);
            
            // While there is an item in deptName column of tblDepartments
            while(res.next()){
                // Concatenate the current deptName to department array of current index
                
                departments.set(i, departments.get(i) + " " + res.getString(1));
                
                // Increment the index of the array
                i++;
            }
            
            
            return new JComboBox(departments.toArray());
        } catch (Exception e){
            System.err.println(e);
        }
        return null;
    }
public JComboBox getTeacherComboBox(String dept, JComboBox combo){
       ArrayList<String> teachers = new ArrayList<String>();
       
        teachers.add("Teacher");
        // Try to access the database, otherwise, catch the exception
        try{
            String driver = "com.mysql.jdbc.Driver";
            Class.forName(driver);
        
            String url = "jdbc:mysql://pgsist.org:3306/2008_itp0";
            String userNameSQL = "itp0";
            String passwordSQL = "frebre3w";
            String teacherIDQuery = "Select teacherID " +
                                 "FROM tblTeachers " +
                                 "WHERE deptID=" + dept;
            String teacherNameQuery = "Select lastName " +
                               "FROM tblTeachers " +
                               "WHERE deptID=" + dept;
            
            
            System.out.println("Connecting to database...");
            Connection conn = DriverManager.getConnection(url, userNameSQL, passwordSQL);
            System.out.println("Done.");
        
            Statement st = conn.createStatement();
            
            ResultSet res = st.executeQuery(teacherIDQuery);
            
            // Count the number of departments
            int i = 0;
            
            // While there is an item in deptID column of tblDepartments
            while(res.next()){
                // Add the current deptID to department array of current index
                teachers.add( res.getString(1));
                // Increment the index of the array
                i++;
            } 
            
            // Set i back to zero
            i = 1;
            res = st.executeQuery(teacherNameQuery);
            
            // While there is an item in deptName column of tblDepartments
            while(res.next()){
                // Concatenate the current deptName to department array of current index
                
                teachers.set(i, teachers.get(i) + " " + res.getString(1));
                
                // Increment the index of the array
                i++;
            }
            
            combo.removeAllItems();
            
            for (int a=0;a<teachers.size();a++)
                combo.addItem(teachers.get(a));
            //return new JComboBox(teachers.toArray());
            return combo;
        } catch (Exception e){
            System.err.println(e);
        }
        System.out.println("PROBLEM");
        return null;
    }
public JComboBox getClassComboBox(String dept, JComboBox combo){
       ArrayList<String> classes = new ArrayList<String>();
       
        classes.add("Class");
        // Try to access the database, otherwise, catch the exception
        try{
            String driver = "com.mysql.jdbc.Driver";
            Class.forName(driver);
        
            String url = "jdbc:mysql://pgsist.org:3306/2008_itp0";
            String userNameSQL = "itp0";
            String passwordSQL = "frebre3w";
            String teacherIDQuery = "Select courseID " +
                                 "FROM tblCourses " +
                                 "WHERE deptID=" + dept;
            String teacherNameQuery = "Select courseName " +
                               "FROM tblCourses " +
                               "WHERE deptID=" + dept;
            
            
            System.out.println("Connecting to database...");
            Connection conn = DriverManager.getConnection(url, userNameSQL, passwordSQL);
            System.out.println("Done.");
        
            Statement st = conn.createStatement();
            
            ResultSet res = st.executeQuery(teacherIDQuery);
            
            // Count the number of departments
            int i = 0;
            
            // While there is an item in deptID column of tblDepartments
            while(res.next()){
                // Add the current deptID to department array of current index
                classes.add( res.getString(1));
                // Increment the index of the array
                i++;
            } 
            
            // Set i back to zero
            i = 1;
            res = st.executeQuery(teacherNameQuery);
            
            // While there is an item in deptName column of tblDepartments
            while(res.next()){
                // Concatenate the current deptName to department array of current index
                
                classes.set(i, classes.get(i) + " " + res.getString(1));
                
                // Increment the index of the array
                i++;
            }
            combo.removeAllItems();
            for (int a=0;a<classes.size();a++){
                combo.addItem(classes.get(a));
               
            }
            //return new JComboBox(teachers.toArray());
            return combo;
        } catch (Exception e){
            System.err.println(e);
        }
        return null;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        DepartmentComboBox = getDepartmentComboBox();
        jLabel1 = new javax.swing.JLabel();
        TeacherComboBox = getTeacherComboBox(""+
            smv.filters[1], new JComboBox());
        PeriodComboBox = new javax.swing.JComboBox();
        ClassComboBox = getClassComboBox(""+smv.filters[2],new JComboBox());
        FilterButton = new javax.swing.JButton();
        ResetButton = new javax.swing.JButton();
        EditScheduleButton = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        LogoutButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(schedule_master.Schedule_MasterApp.class).getContext().getResourceMap(HomePage.class);
        jPanel1.setBackground(resourceMap.getColor("jPanel1.background")); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        DepartmentComboBox.setActionCommand(resourceMap.getString("DepartmentComboBox.actionCommand")); // NOI18N
        DepartmentComboBox.setName("DepartmentComboBox"); // NOI18N
        DepartmentComboBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                DepartmentComboBoxItemStateChanged(evt);
            }
        });
        DepartmentComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DepartmentComboBoxActionPerformed(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        TeacherComboBox.setToolTipText("It's where the teachers at.");
        TeacherComboBox.setEnabled(false);
        TeacherComboBox.setName("TeacherComboBox"); // NOI18N

        PeriodComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Period", "Period 1", "Period 2", "Period 3", "Period 4", "Period 5", "Period 6", "Period 7", "Period 8" }));
        PeriodComboBox.setEnabled(false);
        PeriodComboBox.setName("PeriodComboBox"); // NOI18N

        ClassComboBox.setEnabled(false);
        ClassComboBox.setName("ClassComboBox"); // NOI18N

        FilterButton.setText(resourceMap.getString("FilterButton.text")); // NOI18N
        FilterButton.setEnabled(false);
        FilterButton.setName("FilterButton"); // NOI18N
        FilterButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FilterButtonActionPerformed(evt);
            }
        });

        ResetButton.setText(resourceMap.getString("ResetButton.text")); // NOI18N
        ResetButton.setName("ResetButton"); // NOI18N
        ResetButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ResetButtonActionPerformed(evt);
            }
        });

        EditScheduleButton.setText(resourceMap.getString("EditScheduleButton.text")); // NOI18N
        EditScheduleButton.setName("EditScheduleButton"); // NOI18N
        EditScheduleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EditScheduleButtonActionPerformed(evt);
            }
        });

        jPanel5.setBackground(resourceMap.getColor("jPanel5.background")); // NOI18N
        jPanel5.setMaximumSize(new java.awt.Dimension(955, 600));
        jPanel5.setName("jPanel5"); // NOI18N

        jLabel7.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel2.setIcon(resourceMap.getIcon("jLabel2.icon")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        LogoutButton.setText(resourceMap.getString("LogoutButton.text")); // NOI18N
        LogoutButton.setName("LogoutButton"); // NOI18N
        LogoutButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LogoutButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel2)
                        .addGap(397, 397, 397)
                        .addComponent(LogoutButton))
                    .addComponent(jLabel7))
                .addContainerGap(637, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(LogoutButton)
                        .addComponent(jLabel2)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setFont(resourceMap.getFont("jLabel5.font")); // NOI18N
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setFont(resourceMap.getFont("jLabel6.font")); // NOI18N
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(103, 103, 103)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4)
                    .addComponent(jLabel6)
                    .addComponent(jLabel3))
                .addGap(457, 457, 457))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(DepartmentComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TeacherComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ClassComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(PeriodComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(FilterButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ResetButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(EditScheduleButton)
                .addContainerGap(430, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DepartmentComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(FilterButton)
                    .addComponent(ResetButton)
                    .addComponent(EditScheduleButton)
                    .addComponent(jLabel1)
                    .addComponent(TeacherComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ClassComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(PeriodComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(60, 60, 60)
                .addComponent(jLabel3)
                .addGap(45, 45, 45)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addContainerGap(229, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

private void LogoutButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LogoutButtonActionPerformed
// TODO add your handling code here:
    smv.getFrame().setVisible(true);
   //Schedule_MasterApp.getApplication().show(new Schedule_MasterView(Schedule_MasterApp.getApplication()));
    kill();
}//GEN-LAST:event_LogoutButtonActionPerformed

private void FilterButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FilterButtonActionPerformed
JFrame frame= new JFrame("Filterification Time");
smv.setFilters(new int[]{DepartmentComboBox.getSelectedIndex(),
        TeacherComboBox.getSelectedIndex(),
        ClassComboBox.getSelectedIndex(),
        PeriodComboBox.getSelectedIndex()}
        );
  frame.add( new Filter(smv));
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setVisible(true);
frame.pack();

frame.setVisible(true);
kill();// TODO add your handling code here:
}//GEN-LAST:event_FilterButtonActionPerformed

private void ResetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ResetButtonActionPerformed
/*JFrame frame= new JFrame("Welcome Again");
  frame.add( new HomePage(smv));
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setVisible(true);
frame.pack();
 */
    DepartmentComboBox.setSelectedIndex(0);
        TeacherComboBox.setSelectedIndex(0);
        ClassComboBox.setSelectedIndex(0);
        PeriodComboBox.setSelectedIndex(0);
    
    
 //   kill();// TODO add your handling code here:
}//GEN-LAST:event_ResetButtonActionPerformed

private void DepartmentComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DepartmentComboBoxActionPerformed
// TODO add your handling code here:
    
     System.out.println("Action");
     if (!DepartmentComboBox.getSelectedItem().toString().equals("Department")){
        System.out.println("didn't.");
        String s="";
        try{
    s+=  (Integer.parseInt(DepartmentComboBox.getSelectedItem().toString().substring(0, 2)));
    }
    catch(java.lang.NumberFormatException nfe){
        s+=  (Integer.parseInt(DepartmentComboBox.getSelectedItem().toString().substring(0, 1)));
    
    }
        smv.currentDeptID=Integer.parseInt(s);
        TeacherComboBox=getTeacherComboBox(s, TeacherComboBox);
        ClassComboBox=getClassComboBox(s, ClassComboBox);
    }
    else 
        System.out.println("Entered in departments.");
      TeacherComboBox.setEnabled(DepartmentComboBox.getSelectedItem()!="Department");
     PeriodComboBox.setEnabled(DepartmentComboBox.getSelectedItem()!="Department");
     ClassComboBox.setEnabled(DepartmentComboBox.getSelectedItem()!="Department");
     FilterButton.setEnabled(DepartmentComboBox.getSelectedItem()!="Department");
    
}//GEN-LAST:event_DepartmentComboBoxActionPerformed

private void EditScheduleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EditScheduleButtonActionPerformed
// TODO add your handling code here:
    JFrame frame= new JFrame("I want to edit some schedules. Let's do it!!!");
  frame.add( new Edit_Schedule(smv));
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setVisible(true);
frame.pack();
    
    kill();
}//GEN-LAST:event_EditScheduleButtonActionPerformed

private void DepartmentComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_DepartmentComboBoxItemStateChanged
// TODO add your handling code here:
    
    //    TeacherComboBox=getTeacherComboBox(DepartmentComboBox.getSelectedItem().toString());
    
    
}//GEN-LAST:event_DepartmentComboBoxItemStateChanged
private void kill(){
    
     ( (JFrame)(this.getTopLevelAncestor())).dispose();
    
}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox ClassComboBox;
    private javax.swing.JComboBox DepartmentComboBox;
    private javax.swing.JButton EditScheduleButton;
    private javax.swing.JButton FilterButton;
    private javax.swing.JButton LogoutButton;
    private javax.swing.JComboBox PeriodComboBox;
    private javax.swing.JButton ResetButton;
    private javax.swing.JComboBox TeacherComboBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel5;
    // End of variables declaration//GEN-END:variables

}
